package cn.txy.system.controller;

import cn.txy.config.R;
import cn.txy.system.annotation.PreAuthorize;
import cn.txy.system.domain.Permission;
import cn.txy.system.query.PermissionQuery;
import cn.txy.system.service.PermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据ID查询权限",sn="permission:getOne",descs = "根据ID查询权限")
    public R findById(@PathVariable("id")Long id){
        return R.ok(permissionService.findById(id));
    }

    @GetMapping
    @PreAuthorize(name = "查询权限",sn="permission:getAll",descs = "查询权限")
    public R findAll(){
        return R.ok(permissionService.findAll());
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name = "根据ID删除权限",sn="permission:delOne",descs = "根据ID删除权限")
    public R deleteById(@PathVariable("id")Long id){
        permissionService.deleteById(id);
        return R.ok();
    }
    @PostMapping("/deleteAll")
    @PreAuthorize(name = "删除权限",sn="permission:delAll",descs = "删除权限")
    public R batchDelete(@RequestBody List<Long> ids){
        permissionService.batchDelete(ids);
        return R.ok();
    }

    @ApiOperation("添加和修改")
    @PostMapping
    @PreAuthorize(name = "添加和修改权限",sn="permission:saveOne",descs = "添加和修改权限")
    public R add(@RequestBody Permission permission){
        if(permission.getId() == null){
            permissionService.add(permission);
        }else {
            permissionService.update(permission);
        }
        return R.ok();
    }
    @PostMapping("/page")
    @PreAuthorize(name = "分页查询权限",sn="permission:page",descs = "分页查询权限")
    public R getPage(@RequestBody PermissionQuery permissionQuery){
        PageHelper.startPage(permissionQuery.getPage(),permissionQuery.getSize());
        List<Permission> permissions = permissionService.queryPage(permissionQuery);
        PageInfo<Permission> pageInfo = new PageInfo<>(permissions);
        return R.ok(pageInfo);
    }
}
